const app = getApp()
const i18n = require('../../utils/i18n/index')

Page({
  data: {
    locale: 'zh_CN',
    t: {},
    intro: null,
    id:''
  },

  onLoad(options) {
    this.setI18nData()
    options.id = options.id?options.id:1
    this.data.id = options.id
    this.loadIntroData(options.id)
  },

  onShow() {
    // 更新导航栏标题
   wx.setNavigationBarTitle({
       title: i18n.getLocale()=='en_US'?'Article Info':'文章详情'
   }); 
  },

  setI18nData() {
    this.setData({
      locale: i18n.getLocale(),
      t: {
        title: i18n.t('intro.title'),
        history: i18n.t('intro.history'),
        mission: i18n.t('intro.mission'),
        vision: i18n.t('intro.vision'),
        contact: i18n.t('intro.contact'),
        address: i18n.t('intro.address'),
        phone: i18n.t('intro.phone'),
        email: i18n.t('intro.email'),
        menu: {
            intro: i18n.t('index.menu.intro'),
            alliance: i18n.t('index.menu.alliance'),
            join: i18n.t('index.menu.join')
          },
      }
    })
  },

  onLanguageChange() {
    this.setI18nData()
    this.loadIntroData()
  },

  loadIntroData(id) {
    wx.request({
      url: app.globalData.baseUrl + 'public/article/'+id,
      success: res => {
        if (res.data.code === 0) {
            var arc = res.data.data.article
            arc.content = arc.content
              // 移除原有 style、width、height 属性
              .replace(/<img[^>]*>/gi, function(match) {
                // 移除 style/width/height 属性
                let newTag = match
                  .replace(/style\s*=\s*(['"])[\s\S]*?\1/gi, '')
                  .replace(/width\s*=\s*(['"])[\s\S]*?\1/gi, '')
                  .replace(/height\s*=\s*(['"])[\s\S]*?\1/gi, '');
                // 添加自适应样式
                if (newTag.indexOf('style=') === -1) {
                  newTag = newTag.replace('<img', '<img style="max-width:100%;height:auto;display:block"');
                }
                return newTag;
              });

              arc.content_en = arc.content_en
              // 移除原有 style、width、height 属性
              .replace(/<img[^>]*>/gi, function(match) {
                // 移除 style/width/height 属性
                let newTag = match
                  .replace(/style\s*=\s*(['"])[\s\S]*?\1/gi, '')
                  .replace(/width\s*=\s*(['"])[\s\S]*?\1/gi, '')
                  .replace(/height\s*=\s*(['"])[\s\S]*?\1/gi, '');
                // 添加自适应样式
                if (newTag.indexOf('style=') === -1) {
                  newTag = newTag.replace('<img', '<img style="max-width:100%;height:auto;display:block"');
                }
                return newTag;
              });
            console.log(arc)
           //正则替换
          this.setData({
            intro: arc
          })
        }
      }
    })
  },

  onJoin:function() {
    wx.navigateTo({
        url: '/pages/mine/join/join'
    })
  },

  // 分享按钮点击
  onShareTap() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

  // 分享
  onShareAppMessage() {
    return {
      title: this.data.intro.title,
      path: `/pages/intro/intro?id=${this.data.intro.id}`
    }
  },
  onShareTimeline() {
    return {
      title: this.data.intro.title,
      path: `/pages/intro/intro?id=${this.data.intro.id}`
    }
  }
}) 